#include "tutoriallevel.h"

TutorialLevel::TutorialLevel(QWidget *parent)
    : LevelScene(QPixmap(RESOURCE_DIR + "/Ben_Tutorial_Map.png"),
                 QImage(RESOURCE_DIR + "/Ben_Tutorial_Mask.png"), parent),
    dialogConditionTimer(this), toolTrialStartTime(0, 0, 0, 0)
{
    // Configure the condition timer
    dialogConditionTimer.setInterval(500);
    connect(&dialogConditionTimer, SIGNAL(timeout()), SLOT(timerUpdate()));

    buildingImage = new GameBackground(QPixmap(RESOURCE_DIR + "/Ben_Tutorial_Buildings.png"), mainScene);
    buildingImage->setZValue(1);

    addBuilding(QPoint(83, 56), QPointF(808, 531), QPixmap(RESOURCE_DIR + "/buildings/Rose_1home1.png"));
    addBuilding(QPoint(113, 55), QPointF(1107, 532), QPixmap(RESOURCE_DIR + "/buildings/Rose_1home4.png"));
    addBuilding(QPoint(83, 85), QPointF(807, 832), QPixmap(RESOURCE_DIR + "/buildings/Rose_1home3.png"));
    addBuilding(QPoint(114, 85), QPointF(1117, 831), QPixmap(RESOURCE_DIR + "/buildings/Rose_1home2.png"));

    for(int i = 0; i < 3; i++)
    {
        addPerson(QPixmap(RESOURCE_DIR + "/people/Dean_Person1.png"), buildings[0]);
        addPerson(QPixmap(RESOURCE_DIR + "/people/Dean_Person2.png"), buildings[0]);
        addPerson(QPixmap(RESOURCE_DIR + "/people/Dean_Person3.png"), buildings[0]);
        addPerson(QPixmap(RESOURCE_DIR + "/people/Dean_Person4.png"), buildings[1]);
        addPerson(QPixmap(RESOURCE_DIR + "/people/Dean_Person5.png"), buildings[1]);
        addPerson(QPixmap(RESOURCE_DIR + "/people/Dean_Person6.png"), buildings[1]);
        addPerson(QPixmap(RESOURCE_DIR + "/people/Dean_Person7.png"), buildings[2]);
        addPerson(QPixmap(RESOURCE_DIR + "/people/Dean_Person8.png"), buildings[2]);
        addPerson(QPixmap(RESOURCE_DIR + "/people/Dean_Person9.png"), buildings[2]);
        addPerson(QPixmap(RESOURCE_DIR + "/people/Dean_Person10.png"), buildings[3]);
        addPerson(QPixmap(RESOURCE_DIR + "/people/Dean_Person11.png"), buildings[3]);
        addPerson(QPixmap(RESOURCE_DIR + "/people/Dean_Person12.png"), buildings[3]);
    }

    QPixmap dialogBackground(RESOURCE_DIR + "/Ben_TestDialogBackground.png");
    QList<QString> dialogButtons;
    dialogButtons.append("Continue");

    dialogs.append(new Dialog("Welcome to Infection Protection!\n"
                              "Let's get started by teaching you how to play.",
                              dialogButtons, dialogBackground, this));
    dialogs.append(new Dialog("Infection Protection takes place in a seemingly calm, peaceful city, "
                              "but unseen and unnoticed, a dangerous virus is starting to infect "
                              "the population. Your job is to prevent the spread of this disease.",
                              dialogButtons, dialogBackground, this));
    dialogs.append(new Dialog("See the people milling about the city? At the beginning of each "
                              "level, a couple of them will be infected. Infected people will "
                              "look green. Let's add a couple of infected people to the game "
                              "and watch what happens.",
                              dialogButtons, dialogBackground, this));
    dialogs.append(new Dialog("Notice how the disease spread, and how the buildings changed color? "
                              "A red building means that at least half the people inside of it are "
                              "infected. This is helpful in knowing where the disease is in order "
                              "to fight it, which we'll do next.",
                              dialogButtons, dialogBackground, this));
    dialogs.append(new Dialog("See the new soap icon in the toolbar? Your tools will appear here as "
                              "you gain them throughout the game. This tool sterilizes buildings. "
                              "To use it, just click the icon and then click on the building you "
                              "want to sterilize. Sterilizing buildings will help prevent disease "
                              "transfer within the building.",
                              dialogButtons, dialogBackground, this));
    dialogs.append(new Dialog("Let's try using this tool. When you click continue, the population "
                              "will be reset to being mostly healthy with an infected person. Try "
                              "to use the sterilization tool to keep the population healthy longer.",
                              dialogButtons, dialogBackground, this));
    dialogs.append(new Dialog("Think you've got the hang of it? Let's look at one more tool that's "
                              "a little different: the quarantine tool. It's used to block off "
                              "a road and isolate parts of the city. To use it, click the roadblock "
                              "icon in the tool bar, and then click on the road you want to block.",
                              dialogButtons, dialogBackground, this));
    dialogs.append(new Dialog("Like before, when you click continue, the population will be reset "
                              "and you will have some time to try out the new tool. See if you can "
                              "keep the population healthier with the quarantine tool.",
                              dialogButtons, dialogBackground, this));

    QList<QString> lastDialogButtons;
    lastDialogButtons.append("Play Game");
    lastDialogButtons.append("Replay Tutorial");
    dialogs.append(new Dialog("Think you're ready to play the game? Or do you want to replay the "
                              "tutorial?",
                              lastDialogButtons, dialogBackground, this));

    // Center the dialogs and connect their buttonClicked() signals
    QPoint dialogPos(
            QApplication::desktop()->screenGeometry().width() / 2 - dialogs.last()->width() / 2,
            QApplication::desktop()->screenGeometry().height() / 2 - dialogs.last()->height() / 2);

    for(int i = 0; i < dialogs.size(); i++)
    {
        connect(dialogs[i], SIGNAL(buttonClicked(int)), SLOT(buttonClicked(int)));
        dialogs[i]->move(dialogPos);
    }

    currentDialog = 0;
    dialogs[currentDialog]->showDialog();
}

void TutorialLevel::buttonClicked(int buttonID)
{
    dialogs[currentDialog]->hideDialog();

    // Add infected people after the 3rd dialog, then wait
    if (currentDialog == 2)
    {
        addInfectedPeople();
        dialogConditionTimer.start();
        return;
    }

    // Add the sterilization button after the 4th dialog
    if (currentDialog == 3)
    {
        sterilizationTool = addTool(5,QString("Sterilize Building"),RESOURCE_DIR + "/buttons/Dean_Sterilize.png");
    }

    // Reset population and add infected people after the 6th dialog, then wait
    if (currentDialog == 5)
    {
        resetPopulation();
        addInfectedPeople();
        dialogConditionTimer.start();
        toolTrialStartTime.start();
        return;
    }

    // Reset population and add infected people after the 8th dialog, then wait
    if (currentDialog == 7)
    {
        resetPopulation();
        addInfectedPeople();
        dialogConditionTimer.start();
        toolTrialStartTime.start();
        return;
    }

    // Check the button clicked on the 9th (last) dialog, and continue accordingly
    if (currentDialog == 8)
    {
        // Play Game button
        if (buttonID == 0)
            endScene(levelScene1);
        // Replay Tutorial
        else
            endScene(levelSceneTutorial);
    }

    showNextDialog();
}

void TutorialLevel::timerUpdate()
{
    // Advance if on 3rd dialog and at least 30 people are infected
    if (currentDialog == 2)
    {
        int currentInfected = 0;
        for(int i = 0; i < people.length(); i++)
        {
            if (people[i]->virus != 0)
                currentInfected++;
        }

        if (currentInfected >= 20)
        {
            dialogConditionTimer.stop();
            showNextDialog();
        }
    }

    // Advance and add quarantine tool if on the 6th and it's been 30 seconds
    if (currentDialog == 5)
    {
        if (toolTrialStartTime.elapsed() >= 30000)
        {
            dialogConditionTimer.stop();
            quarantineTool = addTool(5,QString("Quarantine (Place road block)"),RESOURCE_DIR + "/buttons/Dean_RoadBlock.png");
            showNextDialog();
        }
    }

    // Advance if on the 8th and it's been 30 seconds
    if (currentDialog == 7)
    {
        if (toolTrialStartTime.elapsed() >= 30000)
        {
            dialogConditionTimer.stop();
            showNextDialog();
        }
    }
}

LevelScene::LevelEndState TutorialLevel::getLevelEndState()
{
    return Playing;
}

void TutorialLevel::resetPopulation()
{
    // Clear the population
    for (int i = people.length() - 1; i >= 0; i--)
    {

        if (people[i] != 0)
            removePerson(people[i]);
    }

    // Add the 36 healthy people
    for(int i = 0; i < 3; i++)
    {
        addPerson(QPixmap(RESOURCE_DIR + "/people/Dean_Person1.png"), buildings[0]);
        addPerson(QPixmap(RESOURCE_DIR + "/people/Dean_Person2.png"), buildings[0]);
        addPerson(QPixmap(RESOURCE_DIR + "/people/Dean_Person3.png"), buildings[0]);
        addPerson(QPixmap(RESOURCE_DIR + "/people/Dean_Person4.png"), buildings[1]);
        addPerson(QPixmap(RESOURCE_DIR + "/people/Dean_Person5.png"), buildings[1]);
        addPerson(QPixmap(RESOURCE_DIR + "/people/Dean_Person6.png"), buildings[1]);
        addPerson(QPixmap(RESOURCE_DIR + "/people/Dean_Person7.png"), buildings[2]);
        addPerson(QPixmap(RESOURCE_DIR + "/people/Dean_Person8.png"), buildings[2]);
        addPerson(QPixmap(RESOURCE_DIR + "/people/Dean_Person9.png"), buildings[2]);
        addPerson(QPixmap(RESOURCE_DIR + "/people/Dean_Person10.png"), buildings[3]);
        addPerson(QPixmap(RESOURCE_DIR + "/people/Dean_Person11.png"), buildings[3]);
        addPerson(QPixmap(RESOURCE_DIR + "/people/Dean_Person12.png"), buildings[3]);
    }

    emit startPeople();
}

void TutorialLevel::addInfectedPeople()
{
    addPerson(QPixmap(RESOURCE_DIR + "/people/Dean_Person1.png"), buildings[1]);
    people.last()->virus = new Virus(450,0,0,0,people.last());
}

void TutorialLevel::showNextDialog()
{
    if (currentDialog + 1 < dialogs.size())
    {
        currentDialog++;
        dialogs[currentDialog]->showDialog();
    }
}
